package com.peanuts.tender.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;


@Configuration
@MapperScan(basePackages = {"com.peanuts.tender.dao"}, sqlSessionFactoryRef = "tenderSqlSessionFactory")
public class TenderDataSourceConfig {
    @Value("${datasource.tender.tenderMapperLocations}")
    private String tenderMapperLocations;
    @Value("${datasource.tender.typeAliasesPackage}")
    private String typeAliasesPackage;

    @ConfigurationProperties(prefix = "datasource.tender")
    @Bean(name = "tenderDataSource")
    @Primary
    public DruidDataSource tenderDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        Properties properties = new Properties();
        properties.setProperty("druid.stat.mergeSql", "true");
        properties.setProperty("druid.stat.slowSqlMillis", "10000");
        druidDataSource.setConnectProperties(properties);
        // 配置监控统计拦截的filters，去掉后监控界面sql无法统计，'wall'用于防火墙
        try {
            druidDataSource.setFilters("stat,wall,log4j");
        } catch (SQLException e) {
            System.out.println(e);
            e.printStackTrace();
        }
        return druidDataSource;
    }

    /**
     * SqlSessionFactory配置
     *
     * @return
     * @throws Exception
     */
    @Bean(name = "tenderSqlSessionFactory")
    @Primary
    public SqlSessionFactory tenderSqlSessionFactory(
            @Qualifier("tenderDataSource") DataSource dataSource
    ) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        // 配置mapper文件位置
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources(tenderMapperLocations));
        sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
        //配置分页插件
        PageHelper pageHelper = new PageHelper();
//        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql");
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
//        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        pageHelper.setProperties(properties);
//        pageInterceptor.setProperties(properties);
        pageHelper.setProperties(properties);
        //设置插件
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});
        return sqlSessionFactoryBean.getObject();
    }

    /**
     * 配置事物管理器
     *
     * @return
     */
    @Bean(name = "tenderTransactionManager")
    @Primary
    public DataSourceTransactionManager tenderTransactionManager(
            @Qualifier("tenderDataSource") DruidDataSource tenderDataSource
    ) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(tenderDataSource);
        return dataSourceTransactionManager;
    }
}